/**
 * 
 */
package br.com.ema.maze.factory;

import java.awt.Color;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import br.com.ema.maze.agents.MazeCharacter;
import br.com.ema.maze.agents.WallPassageAllowerAgent;
import br.com.ema.maze.character.strategy.MazeCharacterStrategy;
import br.com.ema.maze.components.Coordinates;
import br.com.ema.maze.components.Maze;
import br.com.ema.maze.components.MazeSpace;
import br.com.ema.maze.enums.ConfigurationType;

/**
 * @author Emanuel Cruz Rodrigues -> emanuelcruzrodrigues@gmail.com
 *
 */
public class MazeConfiguration {
	
	private Coordinates startCoordinates;
	private Coordinates exitCoordinates;
	private int passageAllowersInterval;
	private Map<String, MazeCharacter> characters;
	
	public MazeConfiguration() {
		characters = new HashMap<String, MazeCharacter>();
	}
	
	public Coordinates getStartCoordinates() {
		return startCoordinates;
	}
	public void setStartCoordinates(Coordinates startCoordinates) {
		this.startCoordinates = startCoordinates;
	}
	public Coordinates getExitCoordinates() {
		return exitCoordinates;
	}
	public void setExitCoordinates(Coordinates exitCoordinates) {
		this.exitCoordinates = exitCoordinates;
	}
	public int getPassageAllowersInterval() {
		return passageAllowersInterval;
	}
	public void setPassageAllowersInterval(int passageAllowersInterval) {
		this.passageAllowersInterval = passageAllowersInterval;
	}
	public Map<String, MazeCharacter> getCharacters() {
		return characters;
	}
	public MazeCharacter getCharacter(String characterId){
		return characters.get(characterId);
	}
	public void setCharacters(Map<String, MazeCharacter> characters) {
		this.characters = characters;
	}
	
	public void configure(Maze maze){
		MazeSpace start = maze.getSpace(startCoordinates);
		
		maze.setExit(exitCoordinates);
		MazeSpace exit = maze.getSpace(exitCoordinates);
		
		for (String characterId : characters.keySet()) {
			MazeCharacter character = characters.get(characterId);
			maze.addCharacter(characterId, character);
			
			if (start != null){
				character.setActualSpace(start);
				start.putCharacter(character);
			}
			
			character.setDestination(exit);
		}
		
		if (passageAllowersInterval > 0){
			WallPassageAllowerAgent allower = new WallPassageAllowerAgent(passageAllowersInterval);
			maze.addWallPassageAllower(allower);
			
			Collection<MazeSpace> spaces = maze.getSpaces();
			for (MazeSpace space : spaces) {
				if (space.hasWalls()){
					allower.addWall(space);
				}
			}
		}
		
	}
	
	public void setupCharactersFromMazeParameters(MazeParameters mazeParameters){
		Map<String, MazeCharacter> characters = new HashMap<String, MazeCharacter>();
		
		List<MazeCharacterConfiguration> charactersConfigurations = mazeParameters.getCharactersConfigurations();
		for (MazeCharacterConfiguration characterConfiguration : charactersConfigurations) {
			MazeCharacter character = new MazeCharacter((Long) characterConfiguration.getConfiguration(ConfigurationType.ACTIONS_INTERVAL));
			character.setStrategy((MazeCharacterStrategy) characterConfiguration.getConfiguration(ConfigurationType.MAZE_CHARACTER_STRATEGY));
			character.setColor((Color) characterConfiguration.getConfiguration(ConfigurationType.COLOR));
			characters.put((String) characterConfiguration.getConfiguration(ConfigurationType.CHARACTER_ID), character);
		}

		setCharacters(characters);
	}

}
